package com.hornet.commons.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import tk.mybatis.mapper.common.Mapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hornet.commons.mybatis.BaseMapper;
import com.hornet.commons.spring.SpringContextHolder;
import com.hornet.commons.web.model.AjaxResponse;
import com.hornet.commons.web.model.DataGrid;
import com.hornet.commons.web.model.SystemContext;

public abstract class BaseService<T> implements IService<T> {
	
	@Autowired
	protected Mapper<T> mapper;
	
	@Autowired
	protected BaseMapper<T> baseMapper ;


    public Mapper<T> getMapper() {
        return mapper;
    }

    @Override
    public T selectByKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    @Override
    public int save(T entity) {
        return mapper.insert(entity);
    }

    @Override
    public int delete(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    @Override
    public int updateAll(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    @Override
    public int updateNotNull(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }
    
    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }
    
    @Override
	public AjaxResponse add(T entity) {
    	AjaxResponse ajaxResponse = SpringContextHolder.getBean(AjaxResponse.class).reset() ;
		if(save(entity) > 0) {
			ajaxResponse.setStatus(true);
			ajaxResponse.setMessage("保存成功！");
		} else {
			ajaxResponse.setStatus(false);
			ajaxResponse.setMessage("保存失败！");
		}
		return ajaxResponse;
	}

	@Override
	public AjaxResponse update(T entity) {
		AjaxResponse ajaxResponse = SpringContextHolder.getBean(AjaxResponse.class).reset() ;
		if(updateNotNull(entity) > 0) {
			ajaxResponse.setStatus(true);
			ajaxResponse.setMessage("修改成功！");
		} else {
			ajaxResponse.setStatus(false);
			ajaxResponse.setMessage("修改失败！");
		}
		return ajaxResponse;
	}
	
	
	@Override
	public List<T> selectByPage(T entity) {
		PageHelper.startPage(SystemContext.getPageNum(), SystemContext.getPageSize());
		return this.baseMapper.selectByPage(entity);
	}
	
	@Override
	public DataGrid<T> datagrid(T entity) {
		DataGrid<T> dg = new DataGrid<T>();
		
		PageHelper.startPage(dg.getPageNum(), dg.getPageSize());
		List<T> list = selectByPage(entity) ;
		PageInfo<T> pInfo = new PageInfo<T>(list) ;
		
		dg.setRows(list);
		dg.setTotal(pInfo.getTotal());
        
		return dg;
	}
	
	@Override
	public AjaxResponse deleteBatchByKeys(Long[] ids) {
		AjaxResponse ajaxResponse = SpringContextHolder.getBean(AjaxResponse.class).reset() ;
		int deleteBatchByKeys = this.baseMapper.deleteBatchByKeys(ids); 
		if(deleteBatchByKeys > 0) {
			ajaxResponse.setStatus(true);
			ajaxResponse.setMessage("批量删除成功！");
		} else {
			ajaxResponse.setStatus(false);
			ajaxResponse.setMessage("批量删除失败！");
		}
		return ajaxResponse;
	}
	
}
